/// <reference path="../_all.ts" />

module Application.Services {

    export class ServerService implements Application.Interfaces.Services.IServerService {
        _serverClass: ng.resource.IResourceClass;
        _serverConfigClass: ng.resource.IResourceClass;
        _serverActionClass: ng.resource.IResourceClass; 

        public static $inject = [
            '$http',
            '$resource',
        ];

        constructor(public Http: ng.IHttpService, public Resource: ng.resource.IResourceService) {
            this._serverClass = <Application.Resources.IServerClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('servers/:id'), null, null);
            this._serverConfigClass = <Application.Resources.IServerClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('servers/:id/configs'), null, null);
            this._serverActionClass = <Application.Resources.IServerClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('actions/'), null, null);
        }

        public GetServers(callback: Function) {
            this._serverClass.query(
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public GetServer(server_id: number, callback: Function) {
            this._serverClass.get({ id: server_id }, null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public GetServerWithConfig(server_id: number, callback: Function) {
            this._serverConfigClass.get({ id: server_id }, null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        public PostServer(server: any, callback: Function) {

        }

        public PostServerAction(server_id: number, server_action: string, callback: Function) {
            this._serverActionClass.save(null, { server_id: server_id, action: server_action },
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }
    };
}